# coding=utf-8
# Date: 2021/8/26 9:51
from typing import List


# 执行用时：120 ms, 在所有 Python3 提交中击败了41.57%的用户
# 内存消耗：19.6 MB, 在所有 Python3 提交中击败了93.46%的用户
class Solution:
    def numRescueBoats(self, people: List[int], limit: int) -> int:
        people.sort()

        res = 0
        i, j = len(people) - 1, 0
        while j <= i:
            if j < i and people[i] + people[j] <= limit:
                j += 1
            i -= 1
            res += 1
        return res


if __name__ == "__main__":
    print(Solution().numRescueBoats([3, 2, 2, 1], 3))  # 3
